<template>
	<div class="login-container">
		<div class="login">
			<h3>欢迎登录</h3>
			<el-form :model="ruleForm" ref="ruleForm" :rules="rules" label-width="80px" class="demo-ruleForm">
				<el-form-item label="用户名" prop="username">
					<el-input v-model="ruleForm.username" type="text" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="ruleForm.password" type="password" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="submitForm('ruleForm')" class="loginBtn">登录</el-button>
					<el-button @click="resetForm('ruleForm')" class="resetBtn">重置</el-button>
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>

<script>
import { mapMutations } from 'vuex';
export default {
	name: 'loginView',
	data() {
		var validateName = (rule, value, callback) => {
			if (value === '') {
				callback(new Error('请输入用户名'));
			} else {
				callback();
			}
		};
		var validatePwd = (rule, value, callback) => {
			if (value === '') {
				callback(new Error('请输入密码'));
			} else {
				callback();
			}
		};
		return {
			ruleForm: {
				username: '',
				password: ''
			},
			rules: {
				username: [
					{ validator: validateName, trigger: 'blur' }, {
						required:true,
						min: 3,
						max: 10,
						message: '用户名在3-10长度',
						trigger: 'change'
					}
				],
				password: [
					{ validator: validatePwd, trigger: 'blur' },
					{
						required:true,
						min: 6,
						max: 10,
						message: '密码在6-10长度',
						trigger: 'change'
					}
				]

			}
		}
	},
	methods: {
		...mapMutations('user', ['setUserInfo']),
		submitForm(formName) {
			this.$refs[formName].validate((valid) => {
				if (valid) {
					
					this.$http.login(this.ruleForm).then(res => {
						console.log(res);
						if (res.data.user_id ===7) {
							this.setUserInfo({username:this.ruleForm.username,token:res.data.token})
							this.$message({
								type: 'success',
								message: '登录成功'
							})
							this.$router.push('/main')
						} else {
							this.$message({
								type: 'error',
								message: '用户名密码错误'
							})
						}

					})

				} else {

					return false;
				}
			});
		},
		resetForm(formName) {
			this.$refs[formName].resetFields();
		}
	}
}


</script>

<style lang="less" >
.login-container {
	width: 100%;
	height: 100%;
	background: url(@/assets/xiaokeai.png) no-repeat;
	background-size: cover;
}

.login {
	position: relative;
	width: 350px;
	height: 280px;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -35%);
	border: 1px solid#fff;
	border-radius: 10px;
	background-color: rgba(255, 255, 255, 0.05);
	text-align: center;

	.el-form-item {
		margin-top: 30px;
	}

	.el-form-item__content {
		width: 250px;
	}

	.el-form-item__label {
		color: #fff;
	}

	.el-input__inner {
		background-color: transparent;
		color: #000;
	}

	.loginBtn {
		margin-right: 20px;
	}

	.resetBtn {
		margin-right: 30px;
	}

	.el-input {
		width: 100%;

	}

	h3 {
		text-align: center;
		color: #fff;
	}
}
</style>